package com.funmi.lizip.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Patrol;
import com.funmi.lizip.entity.PatrolTrack;
import com.funmi.lizip.entity.dto.patrolTrack.PatrolTrackAddRequest;
import com.funmi.lizip.entity.dto.patrolTrack.PatrolTrackQueryRequest;
import com.funmi.lizip.entity.vo.AttendanceVo;
import com.funmi.lizip.entity.vo.PatrolTrackStatisticsVo;
import com.funmi.lizip.entity.vo.PatrolTrackVo;
import com.funmi.lizip.service.PatrolTrackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yeyg
 * @since 2022-11-03
 */
@Api(tags = "巡护轨迹模块")
@RestController
@RequestMapping("/patrolTrack")
public class PatrolTrackController {

    @Autowired
    private PatrolTrackService patrolTrackService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "追加执行中的巡护轨迹 | 不用")
    @PutMapping("")
    public R addPatrolTrack(@RequestBody HashMap<String, Object> request){
        if (request == null){
            return R.fail().data("数据为空");
        }
        boolean save = patrolTrackService.savePatrolTrack(request);
        if (save){
            return R.success().data("新增成功");
        }
        return R.success().data("新增失败");
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "护林员上传巡护轨迹")
    @PostMapping("")
    public R insertPatrolTrack(@RequestBody PatrolTrackAddRequest request){
        if (request == null){
            return R.fail().message("数据为空");
        }
        // 验证用户
        QueryWrapper<PatrolTrack> patrolTrackQueryWrapper = new QueryWrapper<>();
        patrolTrackQueryWrapper.eq("ranger_id", request.getRangerId());
        patrolTrackQueryWrapper.ne("status", false);
        PatrolTrack one = patrolTrackService.getOne(patrolTrackQueryWrapper);
        if (ObjectUtils.isEmpty(one)){
            return R.fail().message("没有可以结束的轨迹");
        }
        one.setTrackMsg(request.getTrackMsg());
        one.setStatus(false);
        one.setPatrolDate(LocalDateTime.now());
        boolean save = patrolTrackService.insertPatrolTrack(one);

        if (save){
            return R.success().data("上传成功");
        }
        return R.fail().data("上传失败");
    }


    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据巡护任务id 获取巡护轨迹")
    @GetMapping("/patrol/{id}")
    public R patrol(@PathVariable int id){
        QueryWrapper<PatrolTrack> patrolTrackQueryWrapper = new QueryWrapper<>();
        patrolTrackQueryWrapper.eq("patrol_id",id);
        patrolTrackQueryWrapper.orderByDesc("gmt_create");
        List<PatrolTrack> list = patrolTrackService.list(patrolTrackQueryWrapper);
        if (!ObjectUtils.isEmpty(list) && !ObjectUtils.isEmpty(list.get(0)) &&
            !ObjectUtils.isEmpty(list.get(0).getTrackMsg())){
            return R.success().data(list.get(0).getTrackMsg());
        }
        return R.fail().message("无轨迹数据");
    }


    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取当前正在进行中的轨迹")
    @GetMapping("/personnel/{id}")
    public R personnel(@PathVariable int id){
        QueryWrapper<PatrolTrack> patrolTrackQueryWrapper = new QueryWrapper<>();
        patrolTrackQueryWrapper.eq("ranger_id",id);
        patrolTrackQueryWrapper.eq("status",1);
        PatrolTrack one = patrolTrackService.getOne(patrolTrackQueryWrapper);
        if (ObjectUtils.isEmpty(one)) {
            return R.fail().message("无轨迹数据");
        }
        return R.success().data(one.getTrackMsg());
    }



}

